<html>
<head><meta charset="utf-8"><title>Performance of &lt;Result as Try&gt;::into_result · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html">Performance of &lt;Result as Try&gt;::into_result</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="226084585"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226084585" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226084585">(Feb 12 2021 at 01:55)</a>:</h4>
<p>Doing some profiling on an application, I noticed that  <code>&lt;core::result::Result as core::ops::try::Try&gt;::into_result</code>was higher than I expected (one specific call site was taking up 1.8% / 150 ms of my total run time). I only have one error type in play here, so I wouldn't expect there to be any conversion going on. I also would not expect this location to result in an error multiple times in one program run (although I'm trying to verify that now).</p>
<p>I'm also surprised that <code>into_result</code> isn't inlined.</p>
<p>Are there any specific performance gotchas I should be aware of when using <code>?</code>?</p>
<p>/cc <span class="user-mention" data-user-id="125270">@scottmcm</span> Since you are doing work on the <code>Try</code> trait, I'm curious if you've any knowledge in this area.</p>



<a name="226084776"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226084776" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226084776">(Feb 12 2021 at 01:59)</a>:</h4>
<p>Shouldn't that be an identity function? If it's not getting inlined that seems bad, but in that case it should be a memcpy so I would check if the result type in question is large</p>



<a name="226085052"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226085052" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226085052">(Feb 12 2021 at 02:03)</a>:</h4>
<p><code>std::mem::size_of::&lt;Error&gt;() = 40</code>; not the biggest but not the smallest</p>



<a name="226085190"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226085190" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226085190">(Feb 12 2021 at 02:05)</a>:</h4>
<p>If people want to see my perf recording, I think you can visit <a href="https://share.firefox.dev/3jNYq2L">https://share.firefox.dev/3jNYq2L</a></p>



<a name="226085289"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226085289" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226085289">(Feb 12 2021 at 02:07)</a>:</h4>
<p>You'd want to focus on <code>sxd_pull_parser::Parser::next::{{closure}}</code> — the other big chunk of time is due to unoptimized debug output formatting</p>



<a name="226085628"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226085628" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226085628">(Feb 12 2021 at 02:12)</a>:</h4>
<p><span class="user-mention" data-user-id="271719">@Mario Carneiro</span> :</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="cp">#[inline]</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">into_result</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="bp">self</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="226085634"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226085634" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226085634">(Feb 12 2021 at 02:12)</a>:</h4>
<p>super suspicious</p>



<a name="226086120"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226086120" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226086120">(Feb 12 2021 at 02:22)</a>:</h4>
<p>Assuming that the function in question is <a href="https://github.com/shepmaster/sxd-pull-parser/blob/main/src/lib.rs#L170-L179">https://github.com/shepmaster/sxd-pull-parser/blob/main/src/lib.rs#L170-L179</a>, it doesn't look like there is very much going on in that function, so it might just be a very hot loop</p>



<a name="226086506"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226086506" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226086506">(Feb 12 2021 at 02:28)</a>:</h4>
<p>It's not quite that exact point in time, but morally equivalent.</p>



<a name="226086520"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226086520" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226086520">(Feb 12 2021 at 02:29)</a>:</h4>
<p>Local changes and force-pushes as I'm still in early development ;-)</p>



<a name="226086935"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226086935" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226086935">(Feb 12 2021 at 02:36)</a>:</h4>
<p>There are some performance issues with the desugaring of <code>?</code>, that last I checked were still unresolved. <a href="https://github.com/rust-lang/rust/pull/62672#issuecomment-511478954">https://github.com/rust-lang/rust/pull/62672#issuecomment-511478954</a> mentions it.</p>



<a name="226086978"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226086978" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226086978">(Feb 12 2021 at 02:37)</a>:</h4>
<p>is into_result inlined into the calling function? If so, it could just be an artifact of skid</p>



<a name="226086979"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226086979" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226086979">(Feb 12 2021 at 02:37)</a>:</h4>
<p>I was wondering if a macro that was just <code>return if Err</code> without any conversion possible would help.</p>



<a name="226086994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226086994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226086994">(Feb 12 2021 at 02:37)</a>:</h4>
<p><span class="user-mention" data-user-id="243558">@Steven Fackler</span> "skid"? That's a new term for me.</p>



<a name="226087042"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226087042" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226087042">(Feb 12 2021 at 02:38)</a>:</h4>
<p>Reading <a href="https://easyperf.net/blog/2018/08/29/Understanding-performance-events-skid">https://easyperf.net/blog/2018/08/29/Understanding-performance-events-skid</a> now</p>



<a name="226087066"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226087066" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226087066">(Feb 12 2021 at 02:39)</a>:</h4>
<p>or even just a slightly bad debuginfo mapping</p>



<a name="226087077"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226087077" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226087077">(Feb 12 2021 at 02:39)</a>:</h4>
<p>you might want to look at the actual instructions its reporting in those samples</p>



<a name="226087242"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226087242" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226087242">(Feb 12 2021 at 02:42)</a>:</h4>
<p><strong>If</strong> I'm using <code>perf annotate</code> correctly:</p>
<div class="codehilite"><pre><span></span><code>       │     _ZN73_$LT$core..result..Result$LT$T$C$E$GT$$u20$as$u20$core..ops..try..Try$GT$11into_result17hdf9f833aec1f96fcE():                                                                                                             ▒
  0.63 │ f6:   movups 0xae(%rsp),%xmm0                                                                                                                                                                                                      ▒
       │       movups %xmm0,0x7e(%rsp)                                                                                                                                                                                                      ▒
  0.14 │       movaps 0x90(%rsp),%xmm0                                                                                                                                                                                                      ▒
  0.70 │       movaps 0xa0(%rsp),%xmm1                                                                                                                                                                                                      ▒
 10.38 │       movaps %xmm1,0x70(%rsp)                                                                                                                                                                                                      ▒
  1.95 │       movaps %xmm0,0x60(%rsp)
</code></pre></div>



<a name="226087264"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226087264" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226087264">(Feb 12 2021 at 02:42)</a>:</h4>
<p>So moving some chunkyboi registers</p>



<a name="226087275"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226087275" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226087275">(Feb 12 2021 at 02:42)</a>:</h4>
<p>that looks like a memcpy and into_result wasn't inlined for whatever reason</p>



<a name="226087290"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226087290" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226087290">(Feb 12 2021 at 02:43)</a>:</h4>
<p>iirc serde internally still uses a try!() macro for maybe this kind of reason</p>



<a name="226087308"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226087308" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226087308">(Feb 12 2021 at 02:43)</a>:</h4>
<p>(I think the serde aspect is what Thom linked above)</p>



<a name="226087399"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226087399" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226087399">(Feb 12 2021 at 02:44)</a>:</h4>
<p>This code is adjacent to code that fills up buffers, but the buffer in this run was 16MiB, so it seems like it shouldn't be part of that</p>



<a name="226087511"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226087511" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226087511">(Feb 12 2021 at 02:46)</a>:</h4>
<p>you may want to consider putting a box in your error type to keep the size down since the compiler's pretty bad about eliminating moves currently</p>



<a name="226087776"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226087776" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Tyson Nottingham <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226087776">(Feb 12 2021 at 02:51)</a>:</h4>
<p>Doesn't look like the exact same thing, but I reported this issue with result propagation awhile back -- <a href="https://github.com/rust-lang/rust/issues/81146">#81146</a></p>



<a name="226087795"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226087795" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226087795">(Feb 12 2021 at 02:51)</a>:</h4>
<p><span class="user-mention" data-user-id="243558">@Steven Fackler</span> any rule-of-thumb for how big is too big?</p>



<a name="226087883"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226087883" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226087883">(Feb 12 2021 at 02:53)</a>:</h4>
<blockquote>
<p><code>-Z mir-opt-level=3</code> </p>
</blockquote>
<p>I suppose it's worth trying some of these level of things out as well</p>



<a name="226087911"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226087911" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226087911">(Feb 12 2021 at 02:53)</a>:</h4>
<p>uhhhh isn't opt-level 3 unsound?</p>



<a name="226087979"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226087979" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226087979">(Feb 12 2021 at 02:54)</a>:</h4>
<p>I think you'd just have to profile to see when things are too big</p>



<a name="226088090"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226088090" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226088090">(Feb 12 2021 at 02:56)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result/near/226087911">said</a>:</p>
<blockquote>
<p>uhhhh isn't opt-level 3 unsound?</p>
</blockquote>
<p>sound = slow, dontchaknow</p>



<a name="226088117"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226088117" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226088117">(Feb 12 2021 at 02:57)</a>:</h4>
<p>Writing my own try-like macro reduced the samples attributed to <code>into_result</code> from 276ms to 4.7ms</p>



<a name="226088122"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226088122" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226088122">(Feb 12 2021 at 02:57)</a>:</h4>
<p>So that's promising</p>



<a name="226088137"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226088137" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226088137">(Feb 12 2021 at 02:57)</a>:</h4>
<p>I'd need to do a bit more statistical profiling to see if it actually made things faster overall</p>



<a name="226088428"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226088428" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226088428">(Feb 12 2021 at 03:03)</a>:</h4>
<p>adding opt-level=3 on nightly (so two changes) brings the samples of <code>into_result</code> back up to 30.9 ms <span aria-label="shrug" class="emoji emoji-1f937" role="img" title="shrug">:shrug:</span></p>



<a name="226088945"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226088945" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226088945">(Feb 12 2021 at 03:09)</a>:</h4>
<p>wait, the samples were nonzero even with your own try-like macro? Why is <code>Result::into_result</code> even getting called in that case?</p>



<a name="226089216"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226089216" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226089216">(Feb 12 2021 at 03:10)</a>:</h4>
<p>I did not change every single usage of <code>?</code>, just those closer to the hottest loops (the <code>StringRing</code> struct, specifically)</p>



<a name="226089442"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226089442" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226089442">(Feb 12 2021 at 03:11)</a>:</h4>
<p>I've a strange setup where I'm on macOS and doing perf on a Linux VM; I don't have a good setup to allow using my editor seamlessly across the VM, so any changes are... more painful than they should be.</p>



<a name="226091088"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226091088" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226091088">(Feb 12 2021 at 03:17)</a>:</h4>
<p>And I'm benchmarking on a laptop with all the power and thermal fluctuations that can entail. I'm hoping that hyperfine saves my bacon there (5x warmup runs, 25x profiling runs)</p>
<p>before:</p>
<div class="codehilite"><pre><span></span><code>  Time (mean ± σ):      2.912 s ±  0.192 s    [User: 2.841 s, System: 0.045 s]
  Range (min … max):    2.705 s …  3.382 s    25 runs
</code></pre></div>
<p>after:</p>
<div class="codehilite"><pre><span></span><code>  Time (mean ± σ):      2.906 s ±  0.087 s    [User: 2.838 s, System: 0.044 s]
  Range (min … max):    2.753 s …  3.100 s    25 runs
</code></pre></div>



<a name="226091262"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226091262" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226091262">(Feb 12 2021 at 03:18)</a>:</h4>
<p>so not obviously worth it here.</p>



<a name="226091392"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226091392" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226091392">(Feb 12 2021 at 03:19)</a>:</h4>
<p>I guess profiling will continue more tomorrow. Thanks everyone!</p>



<a name="226093267"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226093267" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226093267">(Feb 12 2021 at 03:27)</a>:</h4>
<p>Amusingly, if the try trait gets stabilized, then serde could define their own <code>ResultLight</code> type that opts out of the automatic error conversion. Might be a nice benefit</p>



<a name="226093950"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226093950" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226093950">(Feb 12 2021 at 03:29)</a>:</h4>
<p>They probably wouldn’t, due to not wanting to require newer</p>



<a name="226095945"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226095945" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226095945">(Feb 12 2021 at 03:59)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result/near/226086979">said</a>:</p>
<blockquote>
<p>I was wondering if a macro that was just <code>return if Err</code> without any conversion possible would help.</p>
</blockquote>
<p>You can definitely avoid some LLVM trouble if you can get away with a macro that's just <code>match ___ { Ok(v) =&gt; v, e =&gt; return e }</code> -- LLVM turns out to be pretty bad at putting enums back together, even without error conversion (demo: <a href="https://rust.godbolt.org/z/cd4js5">https://rust.godbolt.org/z/cd4js5</a>).  There's been some progress on mir-opts to help this (<a href="https://github.com/rust-lang/rust/issues/66855">https://github.com/rust-lang/rust/issues/66855</a>) but you can see from that demo that it's still not quite working reliably.</p>



<a name="226120516"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226120516" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226120516">(Feb 12 2021 at 10:32)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result/near/226087911">said</a>:</p>
<blockquote>
<p>uhhhh isn't opt-level 3 unsound?</p>
</blockquote>
<p>Those should all have been moved behind <code>-Zunsound-mir-opts</code>.</p>



<a name="226129587"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226129587" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226129587">(Feb 12 2021 at 12:19)</a>:</h4>
<blockquote>
<p>And I'm benchmarking on a laptop with all the power and thermal fluctuations that can entail. </p>
</blockquote>
<p>If you're on linux there's a bunch of sysfs knobs you can set to disable and enable it as you need. Or you can use <a href="https://crates.io/crates/criterion-perf-events">https://crates.io/crates/criterion-perf-events</a> to benchmark instruction counts instead of wall time.</p>



<a name="226132255"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226132255" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226132255">(Feb 12 2021 at 12:48)</a>:</h4>
<p>I’m running Linux on a VM; I’ve used <a href="https://crates.io/crates/iai">https://crates.io/crates/iai</a> to get instruction counts as well, but the L2 cache numbers vary wildly so I don’t know how to interpret that.</p>



<a name="226138972"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226138972" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226138972">(Feb 12 2021 at 13:54)</a>:</h4>
<blockquote>
<p>if you can get away with a macro that's just <code>match ___ { Ok(v) =&gt; v, e =&gt; return e }</code></p>
</blockquote>
<p><span class="user-mention" data-user-id="125270">@scottmcm</span> unfortunately I need to do different success types in a lot of these functions, so I need <code>Err(e) =&gt; Err(e)</code> <span aria-label="sad" class="emoji emoji-2639" role="img" title="sad">:sad:</span></p>



<a name="226150020"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226150020" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226150020">(Feb 12 2021 at 15:21)</a>:</h4>
<p>Profiling in a VM is no joy.</p>



<a name="226152421"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226152421" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226152421">(Feb 12 2021 at 15:36)</a>:</h4>
<p>Isn't everything effectively a VM these days anyway :-)</p>



<a name="226170871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226170871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226170871">(Feb 12 2021 at 17:43)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result/near/226138972">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="125270">scottmcm</span> unfortunately I need to do different success types in a lot of these functions, so I need <code>Err(e) =&gt; Err(e)</code> <span aria-label="sad" class="emoji emoji-2639" role="img" title="sad">:sad:</span></p>
</blockquote>
<p>I would have been surprised if <code>e =&gt; return e</code> worked for you, TBH.  But it's a good demonstration of the fundamental problem.</p>



<a name="226196152"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226196152" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226196152">(Feb 12 2021 at 21:06)</a>:</h4>
<p>Well, some other people investigating different avenues have improved performance to about 20% of the previous time (e.g. the last change was ~2000ms to ~450ms). Turns out async is realllllly not a good fit to thread through the core part of this problem.</p>



<a name="226196251"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226196251" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226196251">(Feb 12 2021 at 21:07)</a>:</h4>
<p>So now gonna see if there's another way to allow filling my processing buffer asynchronously but doing all the actual processing in pure sync.</p>



<a name="226196295"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226196295" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226196295">(Feb 12 2021 at 21:07)</a>:</h4>
<p>e.g. by having a special error state that the caller needs to watch for</p>



<a name="226499228"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Performance%20of%20%3CResult%20as%20Try%3E%3A%3Ainto_result/near/226499228" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hyd-dev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result.html#226499228">(Feb 16 2021 at 10:58)</a>:</h4>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result/near/226120516">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="232545">Joshua Nelson</span> <a href="#narrow/stream/122651-general/topic/Performance.20of.20.3CResult.20as.20Try.3E.3A.3Ainto_result/near/226087911">said</a>:</p>
<blockquote>
<p>uhhhh isn't opt-level 3 unsound?</p>
</blockquote>
<p>Those should all have been moved behind <code>-Zunsound-mir-opts</code>.</p>
</blockquote>
<p><code>-Zmir-opt-level=2</code> is already unsound: <a href="https://github.com/rust-lang/rust/issues/79191">#79191</a><br>
I can still reproduce that with <code>nightly-2021-02-13</code>.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>